package utility;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import packing.Box;
import packing.Container;
import packing.State;

public class FileTool {

	static int index = 1;

	public static void writeReport(Container container, State state, int base) {
		FileWriter file;
		try {
			file = new FileWriter("TestFiles/report.txt", true);
			String writerFile = "";
			writerFile = "ขนาด Container : " + container.toString()
					+ "\nBase State is State " + base + "\n";
			writerFile += state.evaluate();
			file.write(writerFile);
			file.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static State readBRSet(String filepath) {
		List<Box> boxInputList = new ArrayList<Box>();
		try {
			// Open the file that is the first
			// command line parameter
			File file = new File(filepath);
			FileInputStream fstream = new FileInputStream(file);
			String instanceNumber = file.getName();
			instanceNumber = instanceNumber.replaceAll("br", "");
			instanceNumber = instanceNumber.replaceAll(".txt", "");

			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			strLine = br.readLine();
			int totalInstance = Integer.parseInt(strLine.trim());
			strLine = br.readLine().trim();
			String[] strArray = strLine.split(" ");
			String problemID = instanceNumber + "/" + strArray[0];
			strArray = br.readLine().trim().split(" ");
			Container container = Container.getInstance(
					Integer.parseInt(strArray[1]),
					Integer.parseInt(strArray[0]),
					Integer.parseInt(strArray[2]));
			int boxtypeNumber = Integer.parseInt(br.readLine().trim());
			for (int j = 0; j < boxtypeNumber; j++) {
				strArray = br.readLine().trim().split(" ");
				String id = strArray[0];
				int length = Integer.parseInt(strArray[1]);
				boolean xR = Boolean.parseBoolean(strArray[2]);
				int width = Integer.parseInt(strArray[3]);
				boolean yR = Boolean.parseBoolean(strArray[4]);
				int height = Integer.parseInt(strArray[5]);
				boolean zR = Boolean.parseBoolean(strArray[6]);
				int price = Integer.parseInt(strArray[7]);
				int pressure = Integer.parseInt(strArray[8]);
				int weight = Integer.parseInt(strArray[9]);
				Box boxinput = new Box(id, width, length, height, pressure,
						weight,xR,yR,zR,price);
				boxInputList.add(boxinput);
			}
			in.close();
			return new State(container, boxInputList);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return null;
	}

	public static void writeBoxPlace(Container container,
			List<State> baseStateList) {

		boolean blnCreated = false;
		try {
			for (int i = 0; i < baseStateList.size(); i++) {
				String boxplacesName = "boxplaces_" + (i + 1)+".txt";
				File f = new File(boxplacesName);
				blnCreated = f.createNewFile();
				String str = "";
				str += container.getLength() + "\n" + container.getWidth()
						+ "\n" + container.getHeight() + "\n";
				str += baseStateList.get(i).getStrBoxplaces();
				FileWriter file = new FileWriter(boxplacesName);
				file.write(str);
				file.close();
			}
		} catch (IOException ioe) {
		}

	}
}
